home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / movefile.zip / MOVEFILE.C < prev    next >
Text File  |  1993-01-04  |  9KB  |  341 lines

  1. /*  SOURCE FILE: MOVEFILE.C  */
  2. /**************************************************************************/
  3. /*  MOVEFILE - File Move Utility  PUBLIC DOMAIN                           */
  4. /*  Original Author: James W. Drash, CompuServe ID [76607,70]             */
  5. /*                                                                        */
  6. /*  This program moves files from one directory to another                */
  7. /*     External Routines Used:                                            */
  8. /*       exparg   - used to expand the source file args (wildcards)       */
  9. /*                                                                        */
  10. /*  Developed: Using Borland Turbo C verion 1.00                          */
  11. /*  Compile Options: Model - Tiny; Code Generation - No Floating Point    */
  12. /**************************************************************************/
  13. #include <stdio.h>
  14. #include <dir.h>
  15. #include <dos.h>
  16. #include <io.h>
  17. #include <fcntl.h>
  18. #include <ctype.h>
  19. #include <errno.h>
  20. #include "exparg.h"
  21.  
  22. void getpname(char *,char *);
  23. void help(char *);
  24. int movem(char *,char *,char *);
  25. int c_break(void);
  26. int newfile(char *,char *,char *);
  27. int targ_there(char *,char *);
  28. int copyem(char *,char *,char *);
  29. int same_drive(char *,char *);
  30.  
  31. static char version[] = {"File Move Utility, Version 1.20, " \
  32.                            "PUBLIC DOMAIN"};
  33. main(argc,argv)
  34. int argc;
  35. char *argv[];
  36. {
  37.   static char pgm[MAXFILE] = { "movefile" };
  38.   char target[MAXPATH];
  39.   int i, j;
  40.  
  41.   /* get program name from DOS (version 3.00 and later) */
  42.   if (_osmajor >= 3)
  43.           getpname(*argv,pgm);
  44.  
  45.         ctrlbrk(c_break);
  46.         /* print opening banner for program */
  47.         printf("%s - %s\n\n",pgm,version);
  48.  
  49.   /* if we don't have DOS 2.00 or higher then exit  */
  50.   if (_osmajor < 2) {
  51.           printf("%s: Requires PC/MS DOS 2.00 or higher\n",\
  52.                  "DOS version %d.%d installed\n",pgm,_osmajor,_osminor);
  53.           exit(1);
  54.            }
  55.  
  56.   /* check for right number of arguments, give help if not */
  57.   if (argc < 3 || strcmp(argv[1],"?") == 0) {
  58.     help(pgm);
  59.           exit(1);
  60.     }
  61.  
  62.         /* uppercase everything */
  63.         for (i = 1; i < argc; i ++)
  64.           for (j = 0; j < strlen(argv[i]); j++)
  65.             argv[i][j] = toupper(argv[i][j]);
  66.  
  67.         /* get target */
  68.         strcpy(target,argv[argc - 1]);
  69.         --argc;
  70.  
  71.   /* if target doesn't look like a directory make it */
  72.   if (target[strlen(target) - 1] != '\\')
  73.     strcat(target,"\\");
  74.  
  75.   /* check to see that the target exists */
  76.   if (targ_there(target,pgm) != 0)
  77.           exit(1);
  78.  
  79.  
  80.         /* expand args */
  81.         argv = exparg(&argc,argv);
  82.  
  83.         /* for each file move it */
  84.         for (i = 1; i < argc; i++) {
  85.           /* if same drive then rename should work */
  86.           if (same_drive(argv[i],target) == 0) {
  87.             if (movem(argv[i],target,pgm) == 0)
  88.               printf("%s moved to %s\n",argv[i],target);
  89.             else
  90.               printf("%s: Unable to move - %s\n",pgm,argv[i]);
  91.             }
  92.  
  93.           /* else copy and delete should work */
  94.           else {
  95.             /* if the copy worked try to delete the source */
  96.             if (copyem(argv[i],target,pgm) != 0)
  97.               printf("%s: Unable to move - %s\n",pgm,argv[i]);
  98.             else {
  99.               printf("%s moved to %s\n",argv[i],target);
  100.               /* if the delete falied tell the user */
  101.                if (unlink(argv[i]) != 0)
  102.                 printf("%s",strerror(pgm));
  103.               }
  104.             }
  105.           }
  106.  
  107.   exit(0);
  108. }
  109.  
  110. int c_break(void)
  111. {
  112.   printf("Ctrl-Break hit. Program aborting ...\n");
  113.   fcloseall();
  114.   exit(1);
  115. }
  116.  
  117. void help(name)
  118. char *name;
  119. {
  120.   printf("Usage: %s source ... target\n"  \
  121.          "where source - file(s) to be moved (wildcards allowed)\n" \
  122.          "      target - destination directory\n\n" \
  123.          "multiple source specications allowed\n" \
  124.          "example - %s fu.bar file.* test*.bas \\basic\n\n",name,name);
  125. }
  126.  
  127. void getpname(source,pgm)
  128. char *source;
  129. char *pgm;
  130. {
  131.   char drive[MAXDRIVE];
  132.   char dir[MAXDIR];
  133.   char ext[MAXEXT];
  134.   int i;
  135.  
  136.   /* take apart the source */
  137.   fnsplit(source,drive,dir,pgm,ext);
  138.  
  139.         /* lowercase everything */
  140.         for(i = 0; i < strlen(pgm); i++)
  141.           pgm[i] = tolower(pgm[i]);
  142. }
  143.  
  144. int targ_there(target,pgm)
  145. char *target;
  146. char *pgm;
  147. {
  148.   struct ffblk ffblk;
  149.   char drive[MAXDRIVE];
  150.   char dir[MAXDIR];
  151.   char file[MAXFILE];
  152.   char ext[MAXEXT];
  153.   char targchk[MAXPATH];
  154.   int i;
  155.  
  156.   /* take apart the source */
  157.   fnsplit(target,drive,dir,file,ext);
  158.  
  159.   /* if we're trying to move to the root its ok */
  160.   if (strcmp(dir,"\\") == 0)
  161.     return 0;
  162.  
  163.   /* copy the target to the check string and strip off the
  164.      trailing "\" */
  165.   strcpy(targchk,target);
  166.   targchk[strlen(targchk) -1] = '\0';
  167.  
  168.   /* try to find the target */
  169.   if (findfirst(targchk,&ffblk,FA_DIREC) == 0)
  170.     return 0;
  171.   else {
  172.           printf("%s",strerror(pgm));
  173.           printf("target: %s\n",target);
  174.     return -1;
  175.     }
  176. }
  177.  
  178. int same_drive(source,target)
  179. char *source;
  180. char *target;
  181. {
  182.   char sdrive[MAXDRIVE];
  183.   char sdir[MAXDIR];
  184.   char sfile[MAXFILE];
  185.   char sext[MAXEXT];
  186.   char tdrive[MAXDRIVE];
  187.   char tdir[MAXDIR];
  188.   char tfile[MAXFILE];
  189.   char text[MAXEXT];
  190.   char curdrive[MAXDRIVE];
  191.   int retcode;
  192.  
  193.   /* get current drive */
  194.   curdrive[0] = 'A' + getdisk();
  195.   curdrive[1] = ':';
  196.   curdrive[2] = '\0';
  197.  
  198.   /* take apart the source and target */
  199.   fnsplit(source,sdrive,sdir,sfile,sext);
  200.   fnsplit(target,tdrive,tdir,tfile,text);
  201.  
  202.   /* if either drive is missing put it in */
  203.   if (strlen(sdrive) == 0)
  204.     strcpy(sdrive,curdrive);
  205.   if (strlen(tdrive) == 0)
  206.     strcpy(tdrive,curdrive);
  207.  
  208.   /* see if the drives are the same */
  209.   if (strcmp(sdrive,tdrive) == 0)
  210.     retcode = 0;
  211.   else
  212.     retcode = -1;
  213.  
  214.   /* put together the source and target */
  215.   fnmerge(source,sdrive,sdir,sfile,sext);
  216.   fnmerge(target,tdrive,tdir,tfile,text);
  217.  
  218.         return retcode;
  219. }
  220.  
  221. int newfile(source,target,targ_fn)
  222. char *source;
  223. char *target;
  224. char *targ_fn;
  225. {
  226.   char sdrive[MAXDRIVE];
  227.   char sdir[MAXDIR];
  228.   char sfile[MAXFILE];
  229.   char sext[MAXEXT];
  230.   char tdrive[MAXDRIVE];
  231.   char tdir[MAXDIR];
  232.   char tfile[MAXFILE];
  233.   char text[MAXEXT];
  234.   char curdrive[MAXDRIVE];
  235.   int flag;
  236.  
  237.   /* take apart the source */
  238.   flag = fnsplit(source,sdrive,sdir,sfile,sext);
  239.  
  240.         /* by this time in the program the program their should be no
  241.            wildcards */
  242.         if (flag & WILDCARDS) {
  243.           targ_fn = '\0';
  244.           return -1;
  245.           }
  246.  
  247.         else {
  248.           /* take apart the target */
  249.           fnsplit(target,tdrive,tdir,tfile,text);
  250.           /* put together the new target filename */
  251.           fnmerge(targ_fn,tdrive,tdir,sfile,sext);
  252.           return 0;
  253.           }
  254. }
  255.  
  256. int movem(source,target,pgm)
  257. char *source;
  258. char *target;
  259. char *pgm;
  260. {
  261.   char targ_fn[MAXPATH];
  262.  
  263.   /* if if we are not able to create a new_file name, bail out */
  264.         if (newfile(source,target,targ_fn) != 0)
  265.           return -1;
  266.  
  267.         if (rename(source, targ_fn) != 0) {
  268.           unlink(targ_fn);
  269.           if (rename(source, targ_fn) != 0) {
  270.             printf("%s",strerror(pgm));
  271.             return -1;
  272.             }
  273.           else
  274.             return 0;
  275.           }
  276.         else
  277.           return 0;
  278. }
  279.  
  280. int copyem(source,target,pgm)
  281. char *source;
  282. char *target;
  283. char *pgm;
  284. {
  285.   int bytes;
  286.   FILE *input, *output;
  287.   char iobuf[BUFSIZ];
  288.   struct ftime instamp;
  289.   struct ftime outstamp;
  290.   char targ_fn[MAXPATH];
  291.   char *outbuf;
  292.   int temp_hand;
  293.  
  294.   /* if if we are not able to create a new_file name, bail out */
  295.         if (newfile(source,target,targ_fn) != 0)
  296.           return -1;
  297.  
  298.          if ((input = fopen(source,"rb+")) == NULL) {
  299.            printf("%s: Unable to open: %s\n",pgm,source);
  300.            fcloseall();
  301.            return -1;
  302.            }
  303.          if ((output = fopen(targ_fn,"wb+")) == NULL) {
  304.            unlink(targ_fn);
  305.            if ((output = fopen(targ_fn,"wb+")) == NULL) {
  306.              printf("%s: Unable to open: %s\n",pgm,targ_fn);
  307.              fcloseall();
  308.              return -1;
  309.                }
  310.            }
  311.            do {
  312.              bytes = fread(iobuf,sizeof(char),BUFSIZ,input);
  313.              if (ferror(input)) {
  314.                printf("%s",strerror(pgm));
  315.                printf("unable to move: %s\n",source);
  316.                fclose(input);
  317.                fclose(output);
  318.                return -1;
  319.                }
  320.              fwrite(iobuf,sizeof(char),bytes,output);
  321.              if (ferror(output)) {
  322.                printf("%s",strerror(pgm));
  323.                printf("unable to move: %s\n",source);
  324.                fclose(input);
  325.                fclose(output);
  326.                return -1;
  327.                }
  328.              } while(!feof(input));
  329.            getftime(fileno(input),&instamp);
  330.            outstamp = instamp;
  331.            fclose(input);
  332.            fclose(output);
  333.            if ((temp_hand = open(targ_fn,O_RDWR|O_BINARY)) > 0) {
  334.              setftime(temp_hand,&outstamp);
  335.              close(temp_hand);
  336.              }
  337.            else
  338.              printf("%s",strerror(pgm));
  339.            return 0;
  340. }
  341.